package com.seecen.myspringboot.controller;

import com.github.pagehelper.Page;
import com.seecen.myspringboot.entity.BookCategory;
import com.seecen.myspringboot.service.BookCategoryService;
import com.seecen.myspringboot.vo.MyPage;
import com.seecen.myspringboot.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bookCategory")
public class BookCategoryController {

    @Autowired
    private BookCategoryService bookCategoryService;


    @PostMapping("/save")
    public Result<BookCategory> saveBookCategory(@RequestBody BookCategory bookCategory) {
        bookCategoryService.save(bookCategory);
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result<BookCategory> deleteBookCategory(@PathVariable int id) {
        Result result = bookCategoryService.delete(id);
        return result;
    }

    @PutMapping("/update")
    public Result<BookCategory> updateBookCategory(@RequestBody BookCategory bookCategory) {
        bookCategoryService.updateById(bookCategory);
        return Result.success();
    }

    @GetMapping("/get")
    public Result<MyPage<BookCategory>> getBookCategory(@RequestBody(required = false) MyPage<BookCategory> myPage) {
        if (myPage == null) myPage = new MyPage<>();
        bookCategoryService.findByParam(myPage);
        Result<MyPage<BookCategory>> result = Result.success(myPage);
        return result;
    }
}
